import {
    createRouter,
    __dynamicImportComponent__
} from "simpleRouter";
import { useUserStore } from "@/store";
import applet from "./other/applet"
import app from "./other/app"
import h5 from "./other/h5"

import { routes } from './routes'

const router = createRouter({
    platform: process.env.VUE_APP_PLATFORM,
    routeNotFound: (to) => {
        return new Promise(async (resolve) => {
            return resolve({
                name: `404`,
                navType: `replaceAll`
            })
        })
    },
    applet,
    app,
    h5,
    routes
})

router.beforeEach(async (to, from) => {
    // console.log(`------- beforeEach守卫执行 ------`)
    // console.log('beforeEach=to=>', to)
    // console.log('beforeEach=from=>', from)
    // console.log(to);
    // if (to.path == '/use') {
    //     return false
    // }
    const { token } = useUserStore();
    /**
     * 鉴权 路由拦截跳转登录页
     */
    if (to.meta.requiresAuth && !token) {
        return {
            name: `login`,
            navType: `replaceAll`
        }
    }
    return true
})

router.beforeResolve(async to => {
    // console.log('beforeResolve=to=>', to)
    // const title = i18n.global.t(to.meta.title.replaceAll("%", ""));
    // console.log(title);
    // uni.setNavigationBarTitle({
    //     title
    // });
})

router.afterEach(async (to, from) => {
    // console.log(`++++++ afterEach守卫执行 +++++++`)
    // console.log('afterEach=to=>', to)
    // console.log('afterEach=from=>', from)
})

router.onError(e => {
    // console.log(e)
})

export default router